package com.zzh.ideas.web.wrapper;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * description： <br>
 * createTime: 2018/3/1311:02 <br>
 *
 * @author zzh
 */
public class SessionWrapper implements HttpSession {

    final HttpSession session;

    private static final Set<String> LOGIN_SESSION_KEYS = new HashSet<>();


    public SessionWrapper(HttpSession session) {
        this.session = session;
    }


    @Override
    public Object getAttribute(String name) {
        Object result = session.getAttribute(name);
        if(LOGIN_SESSION_KEYS.contains(name)) {
            if(result == null) {
                throw new RuntimeException("无登录会话");
            }
        }
        return result;
    }


    @Override
    public Object getValue(String name) {
        Object result = session.getValue(name);
        if(LOGIN_SESSION_KEYS.contains(name)) {
            if(result == null) {
                throw new RuntimeException("无登录会话");
            }
        }
        return result;
    }


    @Override
    public long getCreationTime() {
        return session.getCreationTime();
    }


    @Override
    public String getId() {
        return session.getId();
    }


    @Override
    public long getLastAccessedTime() {
        return session.getLastAccessedTime();
    }


    @Override
    public ServletContext getServletContext() {
        return session.getServletContext();
    }


    @Override
    public void setMaxInactiveInterval(int interval) {
        session.setMaxInactiveInterval(interval);
    }


    @Override
    public int getMaxInactiveInterval() {
        return session.getMaxInactiveInterval();
    }


    @Override
    public HttpSessionContext getSessionContext() {
        return session.getSessionContext();
    }


    @Override
    public Enumeration<String> getAttributeNames() {
        return session.getAttributeNames();
    }


    @Override
    public String[] getValueNames() {
        return session.getValueNames();
    }


    @Override
    public void setAttribute(String name, Object value) {
        session.setAttribute(name, value);
    }


    @Override
    public void putValue(String name, Object value) {
        session.putValue(name, value);
    }


    @Override
    public void removeAttribute(String name) {
        session.removeAttribute(name);
    }


    @Override
    public void removeValue(String name) {
        session.removeValue(name);
    }


    @Override
    public void invalidate() {
        session.invalidate();
    }


    @Override
    public boolean isNew() {
        return session.isNew();
    }
}
